% LOWPASS_DESIGN      IIR cheby2 filter for ECG
% Usage:
% [B, A] = lowpass_design(Fp, Fs, Rp, Rs);
% [B, A] = lowpass_design(Fp, Fs, Rp, Rs, Samp_freq);
%
% Inputs:
% Fp: Pass frequency in Hz
% Fs: Stop frequency in Hz
% Rp: Maximium ripple in the pass band in dB
% Rs: Minimium attenuation in the stop band in dB
% Samp_freq: Sampling frequency of the signal in Hz
% 
% Outputs:
% [B, A]: Coefficients of IIR filter
%
% Warnings:
% Do not overdesign the chebyshev type II filter; filtfilt() may fail
% if the filter order is larger than 5
%
% Debug mode:
% If output variable is not specified, debug mode will automatically 
% turned on.
%
% Filtered signal and Frequencncy response of the notch filter will be 
% drawn in new figure window.
%
% Copyright: Group 8
% Modified at 20th Oct 2010
%-------------------------------------------------------------------------
function [B, A] = lowpass_design(Fp,Fs,Rp,Rs,Samp_freq)
if(nargin < 5)
    Samp_freq=553.625;
end
if(nargin < 4)
    Rs=40;
end
if(nargin < 3)
    Rp=1;
end
if(nargin < 5)
    Fs = Fp*1.1;
end

%Find normalized frequency in pi*radian
Wp = Fp / Samp_freq * 2;
Ws = Fs / Samp_freq * 2;

%generate cheby2 filter
[n Wc] = cheb2ord(Wp,Ws,Rp,Rs);
[B, A]= cheby2(n,Rs,Wc);

%Debug:
if(~nargout)
figure(1)
freqz(B,A,[],Samp_freq);
end

